﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Workbench.Annotations
{
    public class ExecuteAnnotationRequestStage : IAnnotationRequestStage
    {
        private readonly IAnnotationRequestBuilderFactory _requestBuilderFactory;

        public ExecuteAnnotationRequestStage(IAnnotationRequestBuilderFactory requestBuilderFactory)
        {
            this._requestBuilderFactory = requestBuilderFactory;
        }

        public void Process(AnnotationRequestContext context, IAnnotationRequestPipelineTraversalStrategy traversalStrategy)
        {
            if (!context.ContainsResultFor(context.RequestKey))
            {
                //get the request stage corresponding to the context annotation key and execute
                var requestStage = this._requestBuilderFactory.GetRequestStageFor(context.RequestKey);
                requestStage.Process(context, traversalStrategy);
            }
            else
            {
                //annotation has already been fetched so just pass through to the next stage
                traversalStrategy.MoveNext(context);
            }
        }
    }
}
